package com.tiger.life;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 测试等题的计算公式
 * 
 * @author tiger 2011-5-24 上午09:17:01
 */
public class CommonUtil {
	/**
	 * 检查网络是否可用
	 * 
	 * @return
	 */
	public static boolean isNetworkAviable(Activity act) {
		ConnectivityManager nw = (ConnectivityManager) act
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (nw == null)
			return false;
		NetworkInfo netinfo = nw.getActiveNetworkInfo();
		if (netinfo == null) {
			return false;
		} else {
			if (netinfo.isAvailable() || netinfo.isConnected()
					|| netinfo.getState() == NetworkInfo.State.CONNECTED)
				return true;
			else
				return false;
		}
	}

	/**
	 * 在检查网络可用性之后使用，判断网络类型
	 * 
	 * @return
	 */
	public static boolean isWifi(Activity act) {
		ConnectivityManager nw = (ConnectivityManager) act
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = nw.getActiveNetworkInfo();
		if(netinfo.getType()==ConnectivityManager.TYPE_WIFI)
			return true;
		else
			return false;
	}
	/**
	 * String[] key1:key
	 * key2:name
	 * key3:rie_num
	 * tiger
	 * 2011-5-31 下午08:57:53
	 * @param act
	 * @param arrayId
	 * @return
	 */
	public static List<String[]> getListData(Activity act,int arrayId){
		List<String[]> datas=new ArrayList<String[]>();
		//数组
		String[] arrs=act.getResources().getStringArray(arrayId);
		for(String arr:arrs){
			String[] data=arr.split("\\|");
			datas.add(data);
		}
		return datas;
	}
	public interface xingzuoInter{
		public static final String index="index";
		public static final String title="title";
	}
	public interface rumarInter{
		public static final String index="index";
		public static final String title="title";
	}
	public interface calorieInter{
		public static final String zao="zao";
		public static final String wu="wu";
		public static final String wan="wan";
		public static final String man="man";
		public static final String wuman="wuman";
		public static final String result="calorieResult";
		public static final String biaozhun="biaozhun";
		public static final String zongsheru="sumcalorie";
	}
	public interface chestInter{
		public static final String colorSel="colorSel";
		public static final String color_yellow="yellow";
		public static final String color_black="black";
		public static final String color_white="white";
		public static final String color_red="red";
	}
	public interface checkFlagValue{
		public static final int cigar_Flag=0x01;
		public static final int baby_Flag=0x02;
		public static final int weight_Flag=0x03;
		public static final int calorie_Flag=0x04;
		public static final int chest_Flag=0x05;
		public static final int xingzuo_Flag=0x06;
		public static final int eredar_Flag=0x07;
		public static final int rumor_Flag=0x08;
		public static final int dog_Flag=0x09;
	}
	public static final String checkFlag = "checkFlag";
	public static final String ageFlag = "ageFlag";

	public static final String age_years = "age_years";
	public static final String age_days = "age_days";
	public static final String bad_days = "bad_days";
	public static final String bad_hours = "bad_hours";

	public static int getDay(String beginDate, String endDate)
			throws ParseException {
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM");
		Date d1 = sim.parse(beginDate);
		Date d2 = sim.parse(endDate);
		return (int) ((d2.getTime() - d1.getTime()) / (3600L * 1000 * 24));
	}
	
	public static final String weightResult="weightResult";
	public static final String dogResult="dogResult";
	public static final String babyResult="babyResult";
	
	public static String getMostNum(int numA,int numB,int numC,int numD){
		String ret="A";
		int tmp=0;
		if(numA>=numB){
			ret="A";
			tmp=numA;
		}else{
			ret="B";
			tmp=numB;
		}
		if(tmp<numC){
			ret="C";
			tmp=numC;
		}
		if(tmp<numD){
			ret="D";
			tmp=numD;
		}
		return ret;
	}
	/**
	 * 得到asset根目录下文本文件的内容
	 * @author:tiger.chenlin (2011-5-11) 
	 * @file:CommonUtil.java 
	 * @return:String
	 * @param context
	 * @param txtNum
	 * @return
	 */
	public static String getTextByTxt(Context context, String fileName) {
		String text = "";
		AssetManager am = context.getAssets();
		InputStream is = null;
		try {
			is = am.open(fileName);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			StringBuilder sb = new StringBuilder("");
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			text=sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
}
